<?php
require_once 'mysqlconnect.php';

function getAllUser(){//获得所有用户的列表
	global $dbc;
	$users = array ();
	
	$query = "SELECT * FROM user_account";
	$result = mysqli_query ( $dbc, $query );
	$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	while ( $row ) {
		$oneUser = array ();
		$oneUser [0] = $row [0];
		$oneUser [1] = $row [1];
		$oneUser [2] = $row [2];
		$oneUser [3] = $row [3];
		$oneUser [4] = $row [4];
		
		$users [] = $oneUser;
		$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	}
	return $users;
}

function findUser($username){//查找到具体的一个用户
	global $dbc;
	
	$username = escape_data($username);
	$oneUser = array ();
	
	$query = "SELECT * FROM user_account WHERE userid='".$username."'";
	$result = mysqli_query ( $dbc, $query );
	$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	if ( $row ) {
		$oneUser [0] = $row [0];
		$oneUser [1] = $row [1];
		$oneUser [2] = $row [2];
		$oneUser [3] = $row [3];
		$oneUser [4] = $row [4];
	}
	return $oneUser;
}

function deleteUser($username){//删除一个用户，包括删除其所有留言及订单
	global $dbc;
	
	$username = escape_data($username);
	
	$query = "DELETE * FROM user_account WHERE userid='".$username."'";
	$result = mysqli_query ( $dbc, $query );
	if ( $result ) {
		if(deleteUserSheet($username) && deleteUserComment($username)){
			return TRUE;
		}else{
			return FALSE;
		}
	}else{
		return FALSE;
	}
}

function deleteUserSheet($userid){//删除该用户的所有订单
	global $dbc;
	$ok = TRUE;
	
	$userid = escape_data($userid);
	
	$query = "SELECT sheetid FROM shopping_sheet WHERE userid='".$userid."'";
	$result = mysqli_query ( $dbc, $query );
	$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	while($row){
		$query1 = "DELETE * FROM shopping_sheet WHERE sheetid='".$row[0]."'";
		$result1 = mysqli_query ( $dbc, $query1 );
		$query2 = "DELETE * FROM sheet_content WHERE sheetid='".$row[0]."'";
		$result2 = mysqli_query ( $dbc, $query2 );
		
		if(!($result1 && $result2)){
			$ok = FALSE;
			break;
		}
		
		$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	}
	return $ok;
}

function deleteUserComment($userid){//删除用户的所有留言
	global $dbc;
	
	$userid = escape_data($userid);
	
	$query = "DELETE * FROM goods_comment WHERE userid='".$userid."'";
	$result = mysqli_query ( $dbc, $query );
	
	if ( $result ) {
		return TRUE;
	}else{
		return FALSE;
	}
}

function deleteComment($commentid){//删除一条留言
	global $dbc;
	
	$commentid = escape_data($commentid);
	
	$query = "DELETE * FROM goods_comment WHERE gcommentid='".$commentid."'";
	$result = mysqli_query ( $dbc, $query );
	if ( $result ) {
		return TRUE;
	}else{
		return FALSE;
	}
}

function getUserComment($userid){//返回一个用户的留言
	global $dbc;
	$comments = array ();
	
	$query = "SELECT * FROM goods_comment WHERE userid = '".$userid."'";
	$result = mysqli_query ( $dbc, $query );
	$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	while ( $row ) {
		$oneComment = array ();
		$oneComment [] = $row [0];//commentid
		$oneComment [] = $row [1];//userid
		$oneComment [] = $row [2];//content
		$oneComment [] = $row [3];//gid
		
		$query1 = "SELECT * FROM goods_baseinfo WHERE gid='".$row [3]."'";
		$result1 = mysqli_query($dbc, $query1);
		$row1 = mysqli_fetch_array ( $result1, MYSQL_NUM );
		if($row1){
			$oneComment[] = $row1[1];//gname
		}else{
			$oneComment[] = "";
		}
		$oneComment [] = $row [4];//time
		
		$comments [] = $oneComment;
		$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	}
	return $comments;
}
?>